<!DOCTYPE html>

<?php
//    $name='yxy2';
//    $pwd='1212';
//    $request='register';

    $name=$_POST['username'];
    $pwd=$_POST['password'];
    $request=$_POST['select'];

    $HOST="106.13.133.78";
    $USER="chen";
    $PASSWD="5920";
    $DATABASES="cpl";


//    echo '用户名：',$name;
//    echo '密码：',$pwd;
//    print_r($_POST);


if(isset($name)&&isset($pwd)&&$pwd!=''&&$name!=''){
    if('login'==$request) {
        if($conn=mysqli_connect($HOST,$USER,$PASSWD,$DATABASES)){
            mysqli_select_db($conn, "utf8");
            if ($result = mysqli_query($conn, "select pwd from test where name='${name}'")) {
                if ($pwd == mysqli_fetch_row($result)[0]) {
                    mysqli_free_result($result);
                    mysqli_close($conn);
                    header("Location:./iframe/iframe.php?username=${name}");
                    echo 'Login successful';
                    exit;
                } else {
                    echo 'Username/Password error';
                    echo '<br/><a href="./index.php">back</a>';
                }
            } else {

                echo 'search failed';
                echo '<br/><a href="./index.php">back</a>';
            }
            mysqli_free_result($result);
        }else{
            echo 'connected time out';
            echo '<br/><a href="./index.php">back</a>';
        }
        mysqli_close($conn);
    }
    else if('register'==$request){
        if($conn=mysqli_connect($HOST,$USER,$PASSWD,$DATABASES)){
            mysqli_select_db($conn, "utf8");
            //echo  "select name from test where name='${name}'</br>";

            /*************     查询是否已经存在     ****************/
            if($result=mysqli_query($conn,"select name from test where name='${name}'")){
                if(null!=mysqli_fetch_row($result)[0]){
                    echo 'Refusing to register , the name already exist</br>';
                    echo '<br/><a href="./index.php">back</a>';
                    mysqli_free_result($result);
                    mysqli_close($conn);
                    return;
                }else
                    echo 'Allowed to register</br>';
            }else {
                echo 'query failed';
                echo '<br/><a href="./index.php">back</a>';
            }
            /*************     查询是否已经存在     ****************/

            if ($result = mysqli_query($conn, "insert into test(name,phone,age,pwd)values('${name}','null',0,'${pwd}')")) {
                //TODO 为新用户创建目录
                mkdir("./tmp/${name}/",0777,true);
                echo 'register successful </br>';
                echo '<br/><a href="./index.php">back to login</a>';
            } else {
                echo 'query failed';
                echo '<br/><a href="./index.php">back</a>';
            }
            mysqli_free_result($result);
        }else{
            echo 'Connected time out';
            echo '<br/><a href="./index.php">back</a>';
        }
        mysqli_close($conn);
    }

}else{
	echo 'Please input correctly!';
	echo '<br/><a href="./index.php">back</a>';
}

?>
<html>
<head>

    <meta charset="utf-8" />
    <title>get GET value</title>

</head>
<body>
<h1>

</h1>
</body>

</html>

